import 'dart:io';
import 'dart:math';

import 'package:FineArt/components/rounded_button.dart';
import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/user_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/page/imageview/RoundCheckBox.dart';
import 'package:FineArt/page/splash/role_choose.dart';
import 'package:FineArt/page/tab/component/CheckImage.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:FineArt/utils/sputils.dart';
import 'package:badges/badges.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:image_picker/image_picker.dart';
import 'package:package_info/package_info.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:FineArt/core/api/notice_api.dart';
///建议反馈
class FeedBackPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _FeedBackPageState();
  }
}

class _FeedBackPageState extends State<FeedBackPage> {
  String versionName = "";
  String userName;
  Size _size;

  UserInfo userInfo;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      setState(() {
        versionName = packageInfo.version + "." + packageInfo.buildNumber;
      });
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    _size = MediaQuery.of(context).size;
    // SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          toolbarHeight: 0,
          brightness: Brightness.light,
          elevation: 0,
          backgroundColor: Colors.white,
        ),
        backgroundColor: bg_button_gray,
        body: _buildContainer());
  }

  Widget buildTitle() {
    return Container(
      height: getPortraitSHeight(55),
      width: SizeConfig.screenWidth,
      color: Colors.white,
      child: Row(
        children: [
          SizedBox(
            width: getPortraitSWidth(1),
          ),
          InkWell(
            child: Image.asset(
              "assets/images/icon_back_black.png",
              width: getPortraitSWidth(11),
              height: getPortraitSHeight(18),
            ),
            onTap: () {
              Navigator.of(context).pop();
            },
          ),
          SizedBox(
            width: getPortraitSWidth(135),
          ),
          Text(
            "建议反馈",
            style: TextStyle(fontSize: 18, color: text_black),
          ),
        ],
      ),
    );
  }

  Widget _buildContainer() {
    return Container(
        color: Color(0xfffefefe),
        padding: EdgeInsets.symmetric(horizontal: getPortraitSWidth(13)),
        width: _size.width,
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: _buildListChild(),
          ),
        ));
  }

  bool check1 = false;

  List<Widget> _buildListChild() {
    return [
      //头像
      // buildHeader(),
      buildTitle(),
      SizedBox(
        height: getPortraitSHeight(20),
      ),
      Text(
        "问题类型",
        style: TextStyle(fontSize: setSp(16), color: text_black),
      ),
      Row(
        children: [
          SizedBox(width: getPortraitSWidth(20)),
          getCheckBox("素材问题", 1, check1),
          SizedBox(
            width: getPortraitSWidth(40),
          ),
          getCheckBox("用户体验", 2, check1)
        ],
      ),
      Row(
        children: [
          SizedBox(width: getPortraitSWidth(20)),
          getCheckBox("功能问题", 3, check1),
          SizedBox(
            width: getPortraitSWidth(40),
          ),
          getCheckBox("其他", 4, check1)
        ],
      ),
      SizedBox(height: getPortraitSHeight(40)),
      Text(
        "问题描述",
        style: TextStyle(fontSize: setSp(16), color: text_black),
      ),
      buildTextFiled(),
      Row(children: [
        Expanded(child: Text("")),
        Text(
          "限200字符以内",
          style: TextStyle(fontSize: setSp(12), color: Colors.grey),
        )
      ]),
      SizedBox(
        height: getPortraitSHeight(80),
      ),
      buildFinish()
    ];
  }

  Set<int> chooseStyle = Set();
  String title;
  Widget buildFinish() {
    return RoundedButton(
      text: "提交",
      height: getPortraitSHeight(50),
      color: red_main,
      press: () {
        LogUtil.d("submit $_inputValue  $chooseStyle");
        _submitAdvices();
      },
    );
  }

  _submitAdvices(){
    if(title==null || title.length==0){
      ToastUtils.toast("请先选择问题类型");
      return;
    }
    if(_inputValue==null || _inputValue.isEmpty){
      ToastUtils.toast("内容不能为空");
      return;
    }
    AdvicesBodyReq req=new AdvicesBodyReq();
    req.title=title;
    req.content=_inputValue;
    NoticeApi().submitAdvices(req).then((res){
      if(res.code==200){
        ToastUtils.toast("提交成功");
        Navigator.of(context).pop();
      }
      else{
        ToastUtils.toast("提交出错${res.msg}");
      }
    });
  }
  Widget getCheckBox(String text, int style, bool isCheck) {
    return Row(children: [
      RoundCheckBox(
          value: chooseStyle.contains(style),
          onChanged: (bool val) {
            bool contain = chooseStyle.contains(style);
            LogUtil.d("getCheckBox onChange $val $contain");
            setState(() {
              if (val) {
                chooseStyle.clear();
                chooseStyle.add(style);
                title=text;
              } else {
                chooseStyle.remove(style);
              }
            });
          }),
      Text(
        text,
        style: TextStyle(fontSize: 15, color: text_black),
      )
    ]);
  }

  String _inputValue;

  Widget buildTextFiled() {
    return Container(
      height: getPortraitSHeight(142),
      width: SizeConfig.screenWidth,
      decoration: BoxDecoration(
          border: Border.all(color: font_gray, width: 1),
          borderRadius: BorderRadius.all(Radius.circular(10))),
      child: new TextField(
        keyboardType: TextInputType.multiline,
        maxLines: 8,
        //
        onChanged: (val) {
          _inputValue = val;
        },
        style: TextStyle(
          fontSize: 14,
        ),
        decoration: InputDecoration(
          contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
          hintText: '请输入内容',
          // contentPadding: EdgeInsets.all(10),
          hintStyle: TextStyle(fontSize: 14, color: Colors.grey),
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(15),
              borderSide: BorderSide.none),
          filled: true,
          fillColor: Colors.white,
        ),
        inputFormatters: <TextInputFormatter>[
          LengthLimitingTextInputFormatter(200) //限制长度
        ],
      ),
    );
  }
}
